package com.hymjweb.demo.hymj.customer_manage.freight_agreement.detail.service;

import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.detail.bean.BCustomerFeightAgreementDetailPojo;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.detail.mapper.BCustomerFreightAgreementDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.customer_manage.freight_agreement.detail.service
 * @Author: luoge
 * @CreateTime: 2021-01-21 18:05
 * @Description:
 */
@Service
public class BCustomerFeightAgreementDetailService extends BaseService {

    @Autowired
    private BCustomerFreightAgreementDetailMapper bCustomerFreightAgreementDetailMapper;


    @Transactional
    public int batchInsert(List<BCustomerFeightAgreementDetailPojo> detailPojos) {
        if(detailPojos!=null && detailPojos.size()>0){
            return  bCustomerFreightAgreementDetailMapper.batchInsert(detailPojos);
        }
        return SysConstant.INT_ZERO;
    }

    public List<BCustomerFeightAgreementDetailPojo> getDetailListByAgreementFreightId(String agreementFreightId) {
        if(DataTypeUtil.isEmptyStr(agreementFreightId)){
            return null;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("agreementFreightId",agreementFreightId);
        return queryDetailListNoPage(param);
    }


    public List<BCustomerFeightAgreementDetailPojo> queryDetailListNoPage(Map param) {
        return bCustomerFreightAgreementDetailMapper.findAll(param);
    }


    @Transactional(rollbackFor = {Exception.class})
    public int deleteByAgreementFreightId(String freightAgreementId) throws Exception {
        if(DataTypeUtil.isEmptyStr(freightAgreementId)){
            throwException("协议运价明细删除参数错误!");
        }
        return bCustomerFreightAgreementDetailMapper.deleteByAgreementFreightId(freightAgreementId);
    }

    @Transactional(rollbackFor = {Exception.class})
    public int batchDeleteByAgreementFreightIds(String agreementFreightIds) throws Exception {
        if(DataTypeUtil.isEmptyStr(agreementFreightIds)){
            throwException("协议运价明细删除参数错误!");
        }
        return bCustomerFreightAgreementDetailMapper.batchDeleteByAgreementFreightIds(agreementFreightIds);
    }


    public boolean checkSquence(List<BCustomerFeightAgreementDetailPojo> list){
        boolean result = true;
        for(int i=0;i<list.size()-1;i++){
            Integer value1 = list.get(i).getEndValue();
            Integer value2 = list.get(i+1).getStartValue();
            if(value1.intValue()!=value2.intValue()){
                result = false;
                break;
            }
        }
        return result;
    }

}
